/**
 * 
 */
package org.swing.utility.system.cmd;

import org.swing.utility.system.imp.ServiceController;

/**
 * @author lqnhu
 *
 */
public class LinuxServiceController implements ServiceController {

	private final String LINUX_SERVICE_NAME = "mcservice";
	private final String LINUX_SERVICE_CMD = "/etc/init.d/";
	private final String LINUX_SERVICE_START = "start";
	private final String LINUX_SERVICE_STOP = "stop";
	private final String LINUX_SERVICE_STATUS = "status";

	public int checkService() {
		try {
			String[] input = new String[] { LINUX_SERVICE_CMD
					+ LINUX_SERVICE_NAME + " " + LINUX_SERVICE_STATUS };
			String output = CmdUtil.execCmdWithOutput(input);
			System.out.println(output);
			if (output.indexOf("running") != -1) {
				return 1;
			} else if (output.indexOf("stopped") != -1) {
				return 0;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return -1;
	}

	public boolean startService() {
		try {
			if (CmdUtil.execCmd(new String[] { LINUX_SERVICE_CMD
					+ LINUX_SERVICE_NAME + " " + LINUX_SERVICE_START }, true) == 0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return false;
	}

	public boolean stopService() {
		try {
			if (CmdUtil.execCmd(new String[] { LINUX_SERVICE_CMD
					+ LINUX_SERVICE_NAME + " " + LINUX_SERVICE_STOP }, true) == 0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return false;
	}

	public boolean isStartup() {
		return false;
	}

	public boolean isStartupPossible() {
		return false;
	}

	public void setStartup(boolean enabled) {
		// Not available
	}

	public String getCommand() {
		return "sh -c";
	}

	public boolean isShowTrayIcon() {
		return true;
	}

	public boolean isShowServiceTab() {
		return true;
	}

	public void migrate() {
	}

}
